package uk.ac.jisc.depositmo.widgets;

/**
 * Copyright (c) 2011, University of Southampton
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * # Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 * # Redistributions in binary form must reproduce the above copyright notice, 
 *     this list of conditions and the following disclaimer in the documentation 
 *     and/or other materials provided with the distribution.
 * # Neither the name of the University of Southampton nor the names of its
 *     contributors may be used to endorse or promote products derived from this
 *     software without specific prior written permission.
 *  
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Instances of this class represent "About" dialogs that describe the application.
 * @author Mark Borkum <mark.borkum@gmail.com>
 * 
 */
public class AboutDialog {
	/**
	 * Specifies the location of the image that should be used as the application icon.
	 */
	private static final String IMAGE_FILENAME = "jisc_logo.jpg";
	
	private static ImageData IMAGE_DATA;
	
	static {
		// Construct a new image loader.
		final ImageLoader imageLoader = new ImageLoader();
		
		if (true) {
			// Attempt to load the data for the specified image into the array.
			ImageData[] imageDataArray = imageLoader.load(AboutDialog.class.getResourceAsStream(IMAGE_FILENAME));
			
			// If the array is null or empty, then the image has not been loaded correctly.
			if (imageDataArray == null || imageDataArray.length == 0) {
				throw new RuntimeException("Failed to load image: '" + IMAGE_FILENAME + "'");
			} else {
				// Otherwise, cache the first frame of the image.
				IMAGE_DATA = imageDataArray[0];
			}
		}
	}
	
	private Shell shell;
	
	/**
	 * Constructs a new instance of this class given only its parent. It is created with style <code>SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL</code>.
	 * @param parent  a shell which will be the parent of the new instance
	 */
	public AboutDialog(Shell parent) {
		// Construct a new "window" for the dialog.
		final Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		
		// Set the title.
		shell.setText("About" + " " + ApplicationDialog.getAppName());
		
		// Construct a new canvas for the application icon.
		final Canvas canvas = new Canvas(shell, SWT.NONE);
		
		// Construct and paint the application icon.
		final Image image = new Image(shell.getDisplay(), IMAGE_DATA);
		
		// Set the image as the background of the canvas. 
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				e.gc.drawImage(image, 0, 0);
			}
		});
		
		// Resize the canvas to match the bounds of the image.
		canvas.setSize(image.getBounds().width, image.getBounds().height);
		
		// Construct a new region to display the dialog text.
		final Text message = new Text(shell, SWT.LEFT | SWT.BORDER);
		
		message.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
		message.setForeground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
		message.setEditable(false);
		message.setText("=== " + ApplicationDialog.getAppName() + " ===\n" + ApplicationDialog.getAppSummary() + "\n\nVersion: " + ApplicationDialog.getAppVersion());
		message.setSelection(0, 0);
		
		// Construct a new button that closes the "window" when selected. 
		final Button okButton = new Button(shell, SWT.PUSH);
		
		okButton.setText("Close");
		
		okButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				shell.close();
			}
		});
		
		// Associate the "close" button with the "return" key-event. 
		shell.setDefaultButton(okButton);
		
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		gridLayout.makeColumnsEqualWidth = false;
		
		shell.setLayout(gridLayout);
		
		final GridData canvasLayoutData = new GridData(GridData.FILL_VERTICAL | GridData.CENTER);
		
		canvasLayoutData.heightHint = image.getBounds().height;
		canvasLayoutData.widthHint = image.getBounds().width;
		
		canvas.setLayoutData(canvasLayoutData);
		
		final GridData descriptionLayoutData = new GridData(GridData.FILL_BOTH);
		
		message.setLayoutData(descriptionLayoutData);
		
		final GridData okButtonLayoutData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
		
		okButtonLayoutData.horizontalSpan = 2;
		
		okButton.setLayoutData(okButtonLayoutData);
		
		// Pack the widgets.
		shell.pack();
		
		// Center the "window" in the current display.
		shell.setLocation(
			/* x */ (shell.getDisplay().getClientArea().width - shell.getSize().x) / 2,
			/* y */ (shell.getDisplay().getClientArea().height - shell.getSize().y) / 2
		);
		
		this.setShell(shell);
	}

	/**
	 * Sets the receiver's parent shell.
	 * @param parent  a shell which will be the parent of the new instance
	 */
	private void setShell(Shell parent) {
		this.shell = parent;
	}

	/**
	 * Returns the receiver's parent shell.
	 * @return  a shell
	 */
	public Shell getShell() {
		return shell;
	}
}
